﻿//-----------------------------------------------------------------------
// <copyright file="Query.cs" company="Microsoft Corporation">
//     Copyright Microsoft Corporation. All Rights Reserved. This code released under the terms of the Microsoft Public License (MS-PL, http://opensource.org/licenses/ms-pl.html.) This is sample code only, do not use in production environments.
// </copyright>
//-----------------------------------------------------------------------
namespace VisualStudioOnlineOData.Models
{
    using System.Xml.Linq;

    /// <summary>
    ///
    /// </summary>
    public class Query
    {
        private string _name;
        private string _path;
        private string _project;
        private string _queryId;

        /// <summary>
        /// Gets or sets the content.
        /// </summary>
        /// <value>
        /// The content.
        /// </value>
        public QueryContent Content { get; set; }

        /// <summary>
        /// Gets the display name.
        /// </summary>
        /// <value>
        /// The display name.
        /// </value>
        public string DisplayName
        {
            get
            {
                return string.Format("{0} ({1})", Name, Path.Replace(Project + "/", string.Empty).Replace("/" + Name, string.Empty));
            }
        }

        /// <summary>
        /// Gets or sets the identifier.
        /// </summary>
        /// <value>
        /// The identifier.
        /// </value>
        public string Id { get; set; }

        /// <summary>
        /// Gets the name.
        /// </summary>
        /// <value>
        /// The name.
        /// </value>
        public string Name
        {
            get
            {
                if (_name == null)
                {
                    if (Content == null || Content.Properties == null)
                    {
                        return null;
                    }

                    return _name = Content.Properties.Name;
                }

                return _name;
            }
        }

        /// <summary>
        /// Gets the path.
        /// </summary>
        /// <value>
        /// The path.
        /// </value>
        public string Path
        {
            get
            {
                if (_path == null)
                {
                    if (Content == null || Content.Properties == null)
                    {
                        return null;
                    }

                    return _path = Content.Properties.Path;
                }

                return _path;
            }
        }

        /// <summary>
        /// Gets the project.
        /// </summary>
        /// <value>
        /// The project.
        /// </value>
        public string Project
        {
            get
            {
                if (_project == null)
                {
                    if (Content == null || Content.Properties == null)
                    {
                        return null;
                    }

                    return _project = Content.Properties.Project;
                }

                return _project;
            }
        }

        /// <summary>
        /// Gets the query identifier.
        /// </summary>
        /// <value>
        /// The query identifier.
        /// </value>
        public string QueryId
        {
            get
            {
                if (_queryId == null)
                {
                    if (Content == null || Content.Properties == null)
                    {
                        return null;
                    }

                    return _queryId = Content.Properties.Id;
                }

                return _queryId;
            }
        }

        /// <summary>
        /// Gets or sets the title.
        /// </summary>
        /// <value>
        /// The title.
        /// </value>
        public string Title { get; set; }

        /// <summary>
        /// Froms the XML.
        /// </summary>
        /// <param name="xml">The XML.</param>
        /// <returns></returns>
        public static Query FromXML(XElement xml)
        {
            return new Query()
            {
                Id = xml.Attribute("id").Value,
                Title = xml.Attribute("title").Value,
                _name = xml.Attribute("name").Value,
                _project = xml.Attribute("project").Value,
                _path = xml.Attribute("path").Value,
                _queryId = xml.Attribute("queryId").Value,
            };
        }

        /// <summary>
        /// To the XML.
        /// </summary>
        /// <returns></returns>
        public XElement ToXML()
        {
            return new XElement("query",
                new XAttribute("id", this.Id ?? ""),
                new XAttribute("title", this.Title ?? ""),
                new XAttribute("name", this.Name ?? ""),
                new XAttribute("project", this.Project ?? ""),
                new XAttribute("path", this.Path ?? ""),
                new XAttribute("queryId", this.QueryId ?? ""));
        }
    }
}